/**
 * Created by Administrator on 2017/3/2 0002.
 */
var Game={
    create:function () {
        var game={};
        var arr_circle=[];
        game.ui=new PIXI.DisplayObjectContainer();

        // var pageSee=PageSee.create();
        // var pageMC=PageMC.create();
        var pageTouch0=PageTouch0.create();
        var pageMask0=PageMask0.create();
        var pageMask1=PageMask1.create();
        var pageMask2=PageMask2.create();
        var page0=Page0.create();
        var page1=Page1.create();
        var bt_music=Easy.CreateSprite('assets/images/bt_music.png',650,54,0.5,0.5,1);
        bt_music.alpha=1;
        bt_music.interactive=true;
        bt_music.on('pointerdown',musicHandler);
        var tw=TweenMax.to(bt_music.scale,1,{repeat:-1,yoyo:true,x:0.9,y:0.9,ease:Cubic.easeInOut});

        // page1.In()
        // game.ui.addChild(page1.ui);

        pageTouch0.In()
        game.ui.addChild(pageTouch0.ui);
        game.ui.addChild(bt_music);
        volumeChange(1);

        document.addEventListener('Game_PageIn',pageInHandler)

        function musicHandler(e)
        {
            if(bt_music.alpha>0.95)
            {
                tw.pause();
                bt_music.alpha=0.9;
                volumeChange(0);
            }
            else
            {
                bt_music.alpha=1;
                tw.resume();
                volumeChange(1);
            }
        }

        function pageInHandler(e)
        {
            console.log(e.detail);
            if(e.detail=='0')
            {
                game.ui.addChild(page0.ui);
                page0.In();
            }
            else if(e.detail=='1')
            {
                 game.ui.addChild(page1.ui);
                 page1.In();
            }
            else if(e.detail=='mask0')
            {
                game.ui.addChild(pageMask0.ui);
                pageMask0.In();
                game.ui.addChild(bt_music);
            }
            else if(e.detail=='mask1')
            {
                game.ui.addChild(pageMask1.ui);
                pageMask1.In();
                game.ui.addChild(bt_music);
            }
            else if(e.detail=='mask2')
            {
                game.ui.addChild(pageMask2.ui);
                pageMask2.In();
                game.ui.addChild(bt_music);
            }
            else if(e.detail=='pageTouch0')
            {
                game.ui.addChild(pageTouch0.ui);
                pageTouch0.In2('1');
                game.ui.addChild(bt_music);
            }
            // else if(e.detail=='2')
            // {
            //
            // }
        }




        // game.arr_circle=arr_circle;
        //
        // con.addChild(con_circle);
        // var bg_black=new PIXI.Graphics();
        // bg_black.beginFill(0,1);
        // bg_black.drawRect(0,0,740,1136);
        // con_circle.addChild(bg_black);

        // bg_sp.mask=gp;
        // con_circle.interactive=true;
        // con_circle.on('pointerdown',circleDownHandler)

        // function circleDownHandler(e) {
        //
        //     for(var i=0;i<5;i++)
        //     {
        //         var circle=Circle.create();
        //         con_circle.addChild(circle.ui);
        //
        //         circle.ui.x=e.data.global.x/(innerWidth/740);
        //         circle.ui.y=e.data.global.y/(innerHeight/1136);
        //
        //         arr_circle.push(circle);
        //
        //         circle.ui.scale.x=0;
        //         circle.ui.scale.y=0;
        //         TweenMax.to(circle.ui,2,{delay:0.1*i,alpha:0});
        //         TweenMax.to(circle.ui.scale,2,{delay:0.1*i,x:1,y:1,onCompleteParams:[circle],onComplete:function (c) {
        //             c.ui.parent.removeChild(c.ui);
        //             c=null;
        //             arr_circle.splice(arr_circle.indexOf(c),1);
        //             console.log(arr_circle.length);
        //         }});
        //
        //     }
        //
        // }
        //
        // var int=self.setInterval("clock()",50)
        // function clock()
        // {
        //     var t=new Date()
        //     document.getElementById("clock").value=t
        // }

        return game;
    }

}

var ripples=[];
var rippleTarget;
function Ripple(x, y,_rippleTarget) {
    // sprite
    rippleTarget=_rippleTarget;
    this.sprite = new PIXI.Sprite(resource["assets/images/map.png"].texture);
    this.sprite.anchor.set(0.5);
    this.sprite.position.set(x, y);
    this.sprite.scale.set(0.0);
    rippleTarget.addChild(this.sprite);
    // filter
    this.filter = new PIXI.filters.DisplacementFilter(this.sprite);
    console.log('aa')
}
Ripple.prototype.update = function() {

    if(this.sprite.scale.x>15)
    {
        var ripple=ripples.indexOf(this);
        ripples.splice(ripple,1);
        ripple.filter=null;
        rippleTarget.filters = ripples.map(function (element) {
            return element.filter;
        });
    }
    this.sprite.scale.x += 0.05;
    this.sprite.scale.y += 0.05;
}


var ticker=PIXI.ticker.shared;
ticker.add(function (time) {

    for(var i=0; i<ripples.length; i++) {
        ripples[i].update();
    }
});